<template>
  <div class="box">
    <h2>
      商品规格页面<small
        ><el-button type="primary" size="small" round @click="showDialog()"
          >新增规格</el-button
        ></small
      >
    </h2>
    <el-table :data="specs.allSpecsList" style="width: 100%">
      <el-table-column prop="id" label="序号" width="80"></el-table-column>
      <el-table-column
        prop="specsname"
        label="规格名称"
        width="120"
      ></el-table-column>
      <el-table-column prop="attrs" label="规格属性">
        <template v-slot="props">
          <el-tag
            type="success"
            size="small"
            v-for="attr in props.row.attrs"
            :key="attr"
          >
            {{ attr }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="props">
          <el-tag size="small" v-if="props.row.status === 1">启用</el-tag>
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180">
        <template v-slot="props">
          <el-button
            type="success"
            size="small"
            round
            @click="editSpecs(props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            round
            @click="delSpecs(props.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="specs.page"
      v-model:page-size="specs.size"
      :page-sizes="[1, 3, 5, 10, 20, 50, 100]"
      small
      :total="specs.total"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changeSize"
      @current-change="changePage"
    />
    <!-- 弹窗 -->
    <Dialog ref="VDialog"></Dialog>
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
import { ref, reactive, onMounted } from "vue";
// 导入弹窗组件
import Dialog from "./dialog.vue";
import { useRouter } from "vue-router";
// 导入store
import useSpecStore from "../../stores/specs.js";
// 定义
const specs = useSpecStore();

const $router = useRouter();

// 获取数据
onMounted(() => {
  // 调用函数,获取菜单数据
  specs.getSpecsListAction();
  specs.getSpecsTotalAction();
});

// 分页相关函数
function changeSize(size) {
  specs.CHANGE_SIZE(size);
  specs.getSpecsListAction();
}
function changePage(page) {
  // console.log(page);
  specs.CHANGE_PAGE(page);
  specs.getSpecsListAction();
}

// 弹窗变量
const VDialog = ref(null);
// 打开弹窗
const showDialog = () => {
  VDialog.value.dialogVisible = true;
  // console.log(VDialog.value.dialogVisible);
};

//删除
function delSpecs(Specs) {
  // console.log("Specs", Specs);
  ElMessageBox.confirm("确定删除此规格吗？", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 调用删除函数
      specs.delSpecsAction(Specs.id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除此规格",
      });
    });
}
// 编辑
function editSpecs(Specs) {
  // console.log("11111111111111", VDialog.value, Admins.id);
  // 显示弹窗，将数据发送给子组件
  VDialog.value.acceptValue(Specs);
  showDialog();
}
</script>

<style lang="css" scoped>
.el-pagination {
  margin-top: 20px;
  justify-content: center;
}
</style>
